# ESB.CONFIG File
$esbhome = "C:\Program Files (x86)\Microsoft BizTalk ESB Toolkit"
$xmlEsbConfig = New-Object XML
$xmlEsbConfig.Load("$esbhome\esb.config")
$firstservice = @($xmlEsbConfig.configuration.esb.itineraryServices.itineraryService)[0]
$firstprovider = @($xmlEsbConfig.configuration.esb.adapterProviders.adapterProvider)[0]
$firstresolver = @($xmlEsbConfig.configuration.esb.resolvers.resolver)[0]
Write-Host "Current Itinerary Service Count = " $xmlEsbConfig.configuration.esb.itineraryServices.itineraryService.Count.ToString()
Write-Host "Current Adapter Provider Count = " $xmlEsbConfig.configuration.esb.adapterProviders.adapterProvider.Count.ToString()
Write-Host "Current ESB Resolver Count = " $xmlEsbConfig.configuration.esb.resolvers.resolver.Count.ToString()

# ESB Extensions Configuration XML
$esbExt = "C:\DanTFS\BizTalkESB\Solutions\BizTalk.ESB.Extensions"
$xmlEsbExtConfig = New-Object XML
$xmlEsbExtConfig.Load("$esbExt\ESBConfigurationsToAdd.xml")

# TODO: Figure out how to not add nodes if they are already there...

# Add ESB Extension Itinerary Services
foreach($itinerarySvc in $xmlEsbExtConfig.esb.ItineraryServices.itineraryService)
{
    $newSvc = $firstservice.Clone()
    $newSvc.id = $itinerarySvc.id
    $newSvc.name = $itinerarySvc.name
    $newSvc.type = $itinerarySvc.type
    $newSvc.scope = $itinerarySvc.scope
    $newSvc.stage = $itinerarySvc.stage

    # TODO: Only add if the node doesn't exist already
	
    $xmlEsbConfig.configuration.esb.itineraryServices.AppendChild($newSvc)
}

# Add ESB Adapter Providers
foreach($adapterProvider in $xmlEsbExtConfig.esb.adapterProviders.adapterProvider)
{
	$newProvider = $firstprovider.Clone()
    $newProvider.moniker = $adapterProvider.moniker
    $newProvider.name = $adapterProvider.name
    $newProvider.type = $adapterProvider.type
    
    $xmlEsbConfig.configuration.esb.adapterProviders.AppendChild($newProvider)

}

# Add ESB Extension Resolvers
foreach($esbResolver in $xmlEsbExtConfig.esb.resolvers.resolver)
{    
    $newResolver = $xmlEsbConfig.ImportNode($esbResolver,'true')
    $xmlEsbConfig.configuration.esb.resolvers.AppendChild($newResolver)
}

# Update Itinerary OM to V2
# 1. Remove V1
$itineraryOMV1 = $xmlEsbConfig.SelectSingleNode("//itineraryOM")
$itineraryOMV1.ParentNode.RemoveChild($itineraryOMV1)

# 2. Add V2
$itineraryOMV2 = $xmlEsbExtConfig.esb.itineraryOM
$newOM = $xmlEsbConfig.ImportNode($itineraryOMV2,'true')
$xmlEsbConfig.configuration.esb.AppendChild($newOM)


$xmlEsbConfig.Save("$esbExt\new-esb.config")

Write-Host "Current Itinerary Service Count = " $xmlEsbConfig.configuration.esb.itineraryServices.itineraryService.Count.ToString()
Write-Host "Current Adapter Provider Count = " $xmlEsbConfig.configuration.esb.adapterProviders.adapterProvider.Count.ToString()
Write-Host "Current ESB Resolver Count = " $xmlEsbConfig.configuration.esb.resolvers.resolver.Count.ToString()


#ESB ITINERARY DESIGNER EXTENSION MANIFEST ???